//
//  B_MyActivityListVC.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/8/22.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_MyActivityListVC.h"
#import "B_SelectNavView.h"
#import "BaseScrollView.h"
#import "B_MyActivityTVC.h"
#import "B_MySendActivityVC.h"
#import "B_MyJoinActivityVC.h"
#import "B_MyActivityListModel.h"
#import "B_MyJoinActivityListModel.h"


@interface B_MyActivityListVC ()

@property (nonatomic, strong) B_SelectNavView * navCen;

@property (nonatomic, strong) NSArray * navCenterArray;

@property (nonatomic, strong) BaseScrollView * scrollView;

@property (nonatomic, strong) BaseTableView * mainView0;

@property (nonatomic, strong) BaseTableView * mainView1;

@property (nonatomic, strong) NSMutableArray * joinListArray;


@end

@implementation B_MyActivityListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _joinListArray = [NSMutableArray array];
    // 设置导航中间的控件
    [self getData:YES];
    [self getData1:YES];
    _navCenterArray = @[@"发布", @"参与"];
    self.navigationItem.titleView = self.navCen;
    self.view = self.scrollView;
    self.scrollView.viewsArray = @[self.mainView0, self.mainView1];
//    [_mainView0 setTableViewFrameWithTop:5 left:0 right:0 bottom:0];
//    [_mainView1 setTableViewFrameWithTop:5 left:0 right:0 bottom:0];
   
    
   
}

- (B_SelectNavView *)navCen {
    if (!_navCen) {
        WeakSelf(weSelf, self);
        _navCen = [[B_SelectNavView alloc] initWithFrame:CGRectMake(0, 0, 150, 44)];
        _navCen.titleArray = _navCenterArray;
        _navCen.tapActionWithIndex = ^(NSInteger index) {
            //TODO:执行切换活动和话题
            weSelf.scrollView.selectIndex = index;
        };
    }
    return _navCen;
}

- (BaseScrollView *)scrollView {
    if (!_scrollView) {
        WeakSelf(weSelf, self);
        _scrollView = [[BaseScrollView alloc] init];
        _scrollView.baseScroll = ^(UIScrollView *scrollView) {
            [weSelf.navCen setFrameOfLineView:scrollView.contentOffset.x / SCREEN_WIDTH];
        };
        
    }
    return _scrollView;
}

/**
 发布的

 @return view
 */
- (BaseTableView *)mainView0 {
    if (!_mainView0) {
        WeakSelf(weSelf, self);
        _mainView0 = [[BaseTableView alloc] init];
        [_mainView0 setTableViewFrameWithTop:5 left:0 right:0 bottom:0];
        [_mainView0.tableView registerNib:[UINib nibWithNibName:@"B_MyActivityTVC" bundle:nil] forCellReuseIdentifier:@"B_MyActivityTVC"];
        /// 组数
        _mainView0.numberOfSectionsBlock = ^NSInteger(UITableView *tableView) {
            return 1;
        };
        /// 组里的个数
        _mainView0.numberOfRowsBlock = ^NSInteger(UITableView *tableView, NSInteger section) {
            return weSelf.dataArray.count;
        };
        _mainView0.viewForFooterBlock = ^UIView *(UITableView *tableView, NSInteger section) {
            UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
            view.backgroundColor = [UIColor color_main_background_color];
            return view;
        };
        _mainView0.viewForHeaderBlock = ^UIView *(UITableView *tableView, NSInteger section) {
            UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
            view.backgroundColor = [UIColor color_main_background_color];
            return view;
        };
        _mainView0.heightForFooterBlock = ^CGFloat(UITableView *tableView, NSInteger section) {
            return 10;
        };
        _mainView0.heightForRowBlock = ^CGFloat(UITableView *tableView, NSIndexPath *indexPath) {
            return 240;
        };
        _mainView0.cellForRowBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
            ///TODO:在这里取模型赋值
            B_MyActivityListModel * model = weSelf.dataArray[indexPath.row];
            B_MyActivityTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_MyActivityTVC" forIndexPath:indexPath];
            cell.B_mySendView.hidden = NO;
            [cell.B_activityImageView sd_setImageWithURL:[NSURL URLWithString:model.activityCover]];
            cell.B_activityTitle.text = model.activityTitle;
            cell.applyArray = model.imageArray;
            return cell;
        };
        _mainView0.didSelectRow = ^(UITableView *tableView, NSIndexPath *indexPath) {
            B_MyActivityListModel * model = weSelf.dataArray[indexPath.row];
            B_MySendActivityVC * detail = [[B_MySendActivityVC alloc] init];
            //TODO: 在这里需要传话题详情id
            detail.activityId = model.id;
            [weSelf pushNextViewController:detail];

        };
        [_mainView0 setTableViewHeaderFresh:^{
            [weSelf getData:YES];
        }];
        
        [_mainView0 setTableViewFooterFresh:^{
            [weSelf getData:NO];
        }];
    }
    return _mainView0;
}



- (void) getData:(BOOL) isResresh {
    if (isResresh) {
        self.index = 1;
    } else {
        self.index++;
    }
    NSString * url = [NSString stringWithFormat:@"/yearnfor/inter/activity/selectUserCreateActivity/%@/%ld/10", getUserId(), self.index];
    WeakSelf(weSelf, self);
    [NetworkingManager getNetworkRequestWithUrlString:url parameters:nil isCache:isResresh succeed:^(id data) {
        [weSelf.mainView0 endRefresh];
        if (isResresh) {
            [weSelf.dataArray removeAllObjects];
        }
        if ([data[@"code"] isEqualToString:@"20010"]) {
            for (NSDictionary *dictionary in data[@"values"]) {
                B_MyActivityListModel * model = [B_MyActivityListModel mj_objectWithKeyValues:dictionary];
                model.imageArray = [NSMutableArray array];
                for (NSDictionary * dic in model.portraitList) {
                    [model.imageArray addObject:dic[@"portrait"]];
                }
                [weSelf.dataArray addObject:model];
            }
        }
        [weSelf.mainView0.tableView reloadData];
    } fail:^(NSError *error) {
        [weSelf.mainView0 endRefresh];
        [KeyWindow makeToast:@"服务器异常,请稍后重试!"];
    }];
}


/**
 参与的

 @return view
 */
- (BaseTableView *)mainView1 {
    if (!_mainView1) {
        WeakSelf(weSelf, self);
        _mainView1 = [[BaseTableView alloc] init];
        [_mainView1 setTableViewFrameWithTop:5 left:0 right:0 bottom:0];
        [_mainView1.tableView registerNib:[UINib nibWithNibName:@"B_MyActivityTVC" bundle:nil] forCellReuseIdentifier:@"B_MyActivityTVC"];
        /// 组数
        _mainView1.numberOfSectionsBlock = ^NSInteger(UITableView *tableView) {
            return 1;
        };
        /// 组里的个数
        _mainView1.numberOfRowsBlock = ^NSInteger(UITableView *tableView, NSInteger section) {
            return weSelf.joinListArray.count;
        };
        _mainView1.viewForFooterBlock = ^UIView *(UITableView *tableView, NSInteger section) {
            UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
            view.backgroundColor = [UIColor color_main_background_color];
            return view;
        };
        _mainView1.viewForHeaderBlock = ^UIView *(UITableView *tableView, NSInteger section) {
            UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
            view.backgroundColor = [UIColor color_main_background_color];
            return view;
        };
        _mainView1.heightForFooterBlock = ^CGFloat(UITableView *tableView, NSInteger section) {
            return 10;
        };
        _mainView1.heightForRowBlock = ^CGFloat(UITableView *tableView, NSIndexPath *indexPath) {
            return 240;
        };
        _mainView1.cellForRowBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
            B_MyJoinActivityListModel * model = weSelf.joinListArray[indexPath.row];
            ///TODO:在这里取模型赋值
            B_MyActivityTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_MyActivityTVC" forIndexPath:indexPath];
            cell.B_mySendView.hidden = YES;
            [cell.B_activityImageView sd_setImageWithURL:[NSURL URLWithString:model.activityCover]];
            cell.B_activityTitle.text = model.activityTitle;
            [cell.B_userImageView sd_setImageWithURL:[NSURL URLWithString:model.portrait]];
            cell.B_addressLabel.text = model.city;
            
            return cell;
        };
        _mainView1.didSelectRow = ^(UITableView *tableView, NSIndexPath *indexPath) {
            B_MyJoinActivityListModel * model = weSelf.joinListArray[indexPath.row];
            B_MyJoinActivityVC * detail = [[B_MyJoinActivityVC alloc] init];
            detail.activityId = model.id;
            //TODO: 在这里需要传话题详情id
            [weSelf pushNextViewController:detail];
            
        };
        
        [_mainView1 setTableViewHeaderFresh:^{
            [weSelf getData1:YES];
        }];
        
        [_mainView1 setTableViewFooterFresh:^{
            [weSelf getData1:NO];
        }];
    }
    return _mainView1;
}


- (void) getData1:(BOOL) isResresh {
    if (isResresh) {
        self.index = 1;
    } else {
        self.index++;
    }
    NSString * url = [NSString stringWithFormat:@"/yearnfor/inter/activity/userApplyActivity/%@/%ld/10", getUserId(), self.index];
    WeakSelf(weSelf, self);
    [NetworkingManager getNetworkRequestWithUrlString:url parameters:nil isCache:isResresh succeed:^(id data) {
        [weSelf.mainView1 endRefresh];
        if (isResresh) {
            [weSelf.joinListArray removeAllObjects];
        }
        if ([data[@"code"] isEqualToString:@"20010"]) {
            for (NSDictionary *dictionary in data[@"values"]) {
                B_MyJoinActivityListModel * model = [B_MyJoinActivityListModel mj_objectWithKeyValues:dictionary];
                [weSelf.joinListArray addObject:model];
            }
        }
        [weSelf.mainView1.tableView reloadData];
    } fail:^(NSError *error) {
        [weSelf.mainView1 endRefresh];
        [KeyWindow makeToast:@"服务器异常,请稍后重试!"];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
